using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public  class tbemailthongbaoDAL
    {
        private SqlConnection cn = null;
        public tbemailthongbaoDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbemailthongbaoDAL(tbemailthongbaoDAL email )
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbemailthongbao email)
        {
            SqlCommand cm = new SqlCommand("emailthongbao_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@email", email.Email );
            cm.Parameters.AddWithValue("@huy", email .Huy );
            cm.ExecuteNonQuery();
        }
        public void Update(tbemailthongbao email)
        {
            SqlCommand cm = new SqlCommand("emailthongbao_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mathongbao", email .Mathongbao );
            cm.Parameters.AddWithValue("@email", email .Email );
            cm.Parameters.AddWithValue("@huy", email .Huy );
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("emailthongbao_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mathongbao", iD);
            cm.ExecuteNonQuery();
        }
        public tbemailthongbaocollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("emailthong_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbemailthongbaocollection emailCollection = new tbemailthongbaocollection();
            while (dr.Read())
            {
                tbemailthongbao email = new tbemailthongbao();
                email.Mathongbao = int.Parse(dr.GetValue(0).ToString());
                email.Email  = (dr.GetValue(1).ToString());
                email .Huy  = int.Parse(dr.GetValue(2).ToString());
                emailCollection .Add(email );
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return emailCollection  ;
        }
    }
}
